<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择时间" v-decorator="['salaryTime',validatorRules.salaryTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi='false' v-decorator="['name',validatorRules.name]"/>
            </a-form-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-item label="岗位绩效及加班费" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <a-input-number v-decorator="['postPerformance', validatorRules.postPerformance]" placeholder="请输入岗位绩效及加班费" style="width: 100%" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="12" >
            <a-form-item label="绩效考核分数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['performanceAppraisalScore',validatorRules.performanceAppraisalScore]" :trigger-change="true" dictCode="performance_appraisal_score" placeholder="请选择绩效考核得分" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="应出勤天数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['attendanceDay', validatorRules.attendanceDay]" placeholder="请输入应出勤天数" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="实际出勤天数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['actualAttendanceDay', validatorRules.actualAttendanceDay]" placeholder="请输入实际出勤天数" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="报工天数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['workDay', validatorRules.workDay]" placeholder="请输入报工天数" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="全勤奖" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['attendanceBonus', validatorRules.attendanceBonus]" placeholder="请输入全勤奖" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="BM补助" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['bmAllowance', validatorRules.bmAllowance]" placeholder="请输入BM补助" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="管理津贴" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['managementAllowance', validatorRules.managementAllowance]" placeholder="请输入管理津贴" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="奖励金" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['incentivePayment', validatorRules.incentivePayment]" placeholder="请输入奖励金" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="电脑补助" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['computerAllowance', validatorRules.computerAllowance]" placeholder="请输入电脑补助" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="出差补助" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['travelAllowance', validatorRules.travelAllowance]" placeholder="请输入出差补助" style="width: 100%" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-item label="请假" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <a-input-number v-decorator="['askForLeave', validatorRules.askForLeave]" placeholder="请输入请假" style="width: 100%" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="12">
            <a-form-item label="扣款" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['deductMoney', validatorRules.deductMoney]" placeholder="请输入扣款" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="社保年检补缴" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['socialSecurityCheck', validatorRules.socialSecurityCheck]" placeholder="请输入社保年检补缴" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="个税" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['payTaxes', validatorRules.payTaxes]" placeholder="请输入个税" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="其他扣款" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['otherDeductMoney', validatorRules.otherDeductMoney]" placeholder="请输入其他扣款" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'HfWageSalaryVolatilityForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,

        validatorRules: {
          attendanceDay:{
            rules: [
              { required: true, message: '请输入应出勤天数!'},
            ]
          },
          actualAttendanceDay:{
            rules: [
              { required: true, message: '请输入实际出勤天数!'},
            ]
          },
          workDay:{
            rules: [
              { required: true, message: '请输入报工天数!'},
            ]
          },
          salaryTime: {
            rules: [
              { required: true, message: '请输入月份!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入姓名!'},
            ]
          },
          performanceAppraisalScore: {
            rules: [
              { required: true, message: '请选择岗位绩效考核得分!'},
            ]
          },
          postPerformance: {
            rules: [
              { required: true, message: '请输入岗位绩效及加班费!'},
            ]
          },
          incentivePayment: {
            rules: [
              { required: true, message: '请输入奖励金!'},
            ]
          },
          attendanceBonus: {
            rules: [
              { required: true, message: '请输入全勤奖!'},
            ]
          },
          bmAllowance: {
            rules: [
              { required: true, message: '请输入BM补助!'},
            ]
          },
          managementAllowance: {
            rules: [
              { required: true, message: '请输入管理津贴!'},
            ]
          },
          computerAllowance: {
            rules: [
              { required: true, message: '请输入电脑补助!'},
            ]
          },
          travelAllowance: {
            rules: [
              { required: true, message: '请输入出差补助!'},
            ]
          },
          askForLeave: {
            rules: [
              { required: true, message: '请输入请假!'},
            ]
          },
          deductMoney: {
            rules: [
              { required: true, message: '请输入扣款!'},
            ]
          },
          socialSecurityCheck: {
            rules: [
              { required: true, message: '请输入社保年检补缴!'},
            ]
          },
          payTaxes: {
            rules: [
              { required: true, message: '请输入个税!'},
            ]
          },
          otherDeductMoney: {
            rules: [
              { required: true, message: '请输入其他扣款!'},
            ]
          },
        },
        url: {
          add: "/wage/hfWageSalaryVolatility/add",
          edit: "/wage/hfWageSalaryVolatility/edit",
          queryById: "/wage/hfWageSalaryVolatility/queryById"
        },
        fields:['salaryTime','name','attendanceBonus','bmAllowance','managementAllowance','computerAllowance','attendanceDay','actualAttendanceDay','workDay',
          'travelAllowance','askForLeave','deductMoney','socialSecurityCheck','payTaxes','otherDeductMoney','postPerformance','incentivePayment','performanceAppraisalScore']
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,this.fields))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,this.fields))
      },
    }
  }
</script>